package com.flyee.controller.tools.sandtable
{
	import com.flyee.utils.depot.SandTableDepot;
	import com.flyee.viewer.SandTableViewer;
	
	import flash.display.*;
	
	import mx.core.UIComponent;
	
	public class TitleOptionBar extends UIComponent
	{
		private var navigateBar:Bitmap = null;
		private var estateContainer:Sprite = null;
		private var envitContainer:Sprite = null;
		private var layoutContainer:Sprite = null;
		private var estateSelect:Bitmap = null;
		private var envitSelect:Bitmap = null;
		private var layoutSelect:Bitmap = null;
		public function TitleOptionBar()
		{
			init();
		}
		
		private function init():void{
			navigateBar = new Bitmap();
			estateContainer = new Sprite();
			envitContainer = new Sprite();
			layoutContainer = new Sprite();
			envitSelect = new Bitmap();
			estateSelect = new Bitmap();
			layoutSelect = new Bitmap();
			
			navigateBar.bitmapData = SandTableDepot.getInstance().getBitmapData("NavigationBarData");
			envitSelect.bitmapData = SandTableDepot.getInstance().getBitmapData("EnvSelectedData");
			estateSelect.bitmapData = SandTableDepot.getInstance().getBitmapData("EstateSelectedData");
			layoutSelect.bitmapData = SandTableDepot.getInstance().getBitmapData("LayoutSelectedData");
			estateContainer.addChild(SandTableDepot.getInstance().getSimpleButton("RealEstateBtn"));
			envitContainer.addChild(SandTableDepot.getInstance().getSimpleButton("EnvironmentBtn"));
			layoutContainer.addChild(SandTableDepot.getInstance().getSimpleButton("LayoutBtn"));
			
			envitContainer.x = 80;
			estateContainer.x = 15;
			layoutContainer.x = 145;
			envitContainer.y = 0;
			estateContainer.y = 0;
			layoutContainer.y = 0;
			navigateBar.y = 22;

			this.addChild(navigateBar);
			this.addChild(envitContainer);
			this.addChild(estateContainer);
			this.addChild(layoutContainer);
		}
		/**
		 * 改变按钮的状态 
		 * @param type
		 * 
		 */		
		public function setSelectedState(type:String):void{
			clearContainer(envitContainer);
			clearContainer(estateContainer);
			clearContainer(layoutContainer);
			if(type == SandTableViewer.ENVIRONMENT_BUTTON){
				envitContainer.addChild(envitSelect);
				estateContainer.addChild(SandTableDepot.getInstance().getSimpleButton("RealEstateBtn"));
				layoutContainer.addChild(SandTableDepot.getInstance().getSimpleButton("LayoutBtn"));
			}else if(type == SandTableViewer.ESTATE_BUTTON){
				estateContainer.addChild(estateSelect);
				envitContainer.addChild(SandTableDepot.getInstance().getSimpleButton("EnvironmentBtn"));
				layoutContainer.addChild(SandTableDepot.getInstance().getSimpleButton("LayoutBtn"));
			}else if(type == SandTableViewer.LAYOUT_BUTTON){
				layoutContainer.addChild(layoutSelect);
				estateContainer.addChild(SandTableDepot.getInstance().getSimpleButton("RealEstateBtn"));
				envitContainer.addChild(SandTableDepot.getInstance().getSimpleButton("EnvironmentBtn"));
			}
		}
		
		public function getSelectedContainer(type:String):Sprite{
			if(SandTableViewer.ENVIRONMENT_BUTTON == type){
				return envitContainer;
			}else if(type == SandTableViewer.ESTATE_BUTTON){
				return estateContainer;
			}else if(type == SandTableViewer.LAYOUT_BUTTON){
				return layoutContainer;
			}else{
				return null;
			}
		}
		
		public function clearContainer(con:Sprite):void{
			if(con){
				while(con.numChildren > 0){
					con.removeChildAt(0);
				}
			}
		}
		public function clearBitmap(bit:Bitmap):void{
			if(bit){
				if(bit.bitmapData){
					bit.bitmapData.dispose();
					bit.bitmapData = null;
				}
				bit = null;
			}
		}
		
		public function clear():void{
			while(this.numChildren > 0){
				this.removeChildAt(0);
			}
			clearBitmap(envitSelect);
			clearBitmap(estateSelect);
			clearBitmap(layoutSelect);
			clearBitmap(navigateBar);
			clearContainer(envitContainer);
			clearContainer(estateContainer);
			clearContainer(layoutContainer);
			envitSelect = null;
			envitContainer = null;
			estateContainer = null;
			estateSelect = null;
			layoutContainer = null;
			layoutSelect = null;
			navigateBar = null;
		}
	}
}